#!/bin/sh

set -e

# Import export_gnome_env().
. /usr/local/lib/tails-shell-library/gnome.sh

# Get LIVE_USERNAME
. /etc/live/config.d/username.conf

# Get LANG
. /etc/default/locale

# The Tor Browser hardcodes the default profile dir to inside
# ../TorBrowser/Data/Browser/ from the folder storing the
# application.ini file supplied via -app. We can use -profile to load
# it from a different place, but then the Caches directory
# must still exist and be accessible in the above folder.
mkdir -p /usr/local/lib/TorBrowser/Data/Browser/Caches
chmod -R a+rX /usr/local/lib/TorBrowser

until pgrep -u "${LIVE_USERNAME}" '^ibus-daemon' >/dev/null ; do
   sleep 5
done

export LANG
export_gnome_env
sudo -u ${LIVE_USERNAME} xhost +SI:localuser:tor-launcher
gksudo -u tor-launcher /usr/local/bin/tor-launcher -- "$@"
RET=${?}
sudo -u ${LIVE_USERNAME} xhost -SI:localuser:tor-launcher

exit ${RET}
